(function (window, jQuery, undefined) {
    function getIframe(newSetting) {
        this.setting = {
            iframeEle: '',
            steamId: '',
            webUrl: '',
            push_token: '',
            lang: '',
            url: '',
            userId: '',
            platform: '',
            zbtToken: ''
        }
        zbtweb = null;
        zbtwebset = null;
        this.init(newSetting)
    };
    // 初始化
    getIframe.prototype.init = function (newSetting) {
        $.extend(this.setting, newSetting);
        this.create();
    };
    //动态创建
    getIframe.prototype.create = function () {
        var steamId = this.getSteamId();
        var webUrl = this.getweburl();
        var push_token = this.getpushtoken();
        var lang = this.getlang();
        var url = this.geturl();
        var getiframeEle = this.getiframeEle()
        var userId = this.getuserId()
        var platformId = this.getPlatformId()
        var zbtToken = this.getZbtToken()
        if (window.localStorage.getItem('zbtQuote') == 'false') {
            return false
        }
        zbtweb = new WebSocket(webUrl);
        zbtweb.onopen = function () {
            var json = {
                type: 0,
                steamId: steamId,
                accessToken: push_token,
                platform: 'c5game',
                messageType: 0,
                channelType: 88,
                userId: userId,
                roomNo: userId
            }
            zbtweb.send(JSON.stringify(json))
            zbtwebset = setInterval(function () {
                zbtweb.send(JSON.stringify(json))
            }, 50000)
            zbtweb.onmessage = function (evt) {
                var res = JSON.parse(evt.data)
                if (res.result) {
                    var result = JSON.parse(res.result)
                    if (result.type == 'offer' || result.type == 'order') {
                        var zbtwebispass = false
                        if (result.type == 'offer') {
                            var offers = JSON.parse(result.data.offers)
                            for (var i = 0; i < offers.length; i++) {
                                if (offers[i].status != 3 && offers[i].status != 4) {
                                    zbtwebispass = true
                                }
                            }
                        }
                        if (result.type == 'order') {
                            var orders = JSON.parse(result.data.orders)
                            for (var i = 0; i < orders.length; i++) {
                                if (orders[i].status != 0) {
                                    zbtwebispass = true
                                }
                            }
                        }
                        if (zbtwebispass) {
                            var iframeUrl = url + '/platform/default/quote.html?' + 'steamId=' + steamId + '&push_token=' + push_token + '&lang=' + lang + '&userId=' + userId + '&platform=' + platformId + '&zbtToken=' + zbtToken;
                            console.log(iframeUrl)
                            var iframeId = 'zbtQuote';
                            var iframeClass = '';
                            var iframeEle = getiframeEle;
                            var selecttext = "是否自动弹出"
                            if (lang != 'zh-CN') {
                                selecttext = "Auto Pop-up"
                            }
                            var selectdiv = '<div class="zbtQuote-icheck"><input type="checkbox" checked="checked" id="zbtQuote-isauto"><label for="zbtQuote-isauto">' + selecttext + '</label> </div>'
                            var myIframe = '<div class="zbtQuote-fixed">' + selectdiv + '<iframe src="' + iframeUrl + '"id="' + iframeId + '"class="' + iframeClass + '"iframe></div>'

                            if ($('body').find('#zbtQuote').length >= 1) {

                            } else {
                                if (zbtweb) {
                                    zbtweb.close()
                                }
                                clearInterval(zbtwebset)
                                $(iframeEle).append(myIframe)
                                var backdrop = '<div id="zbtQuote-modal-backdrop" class="fade"></div>'
                                $(iframeEle).append(backdrop)
                                setTimeout(function () {
                                    $(iframeEle).find('.zbtQuote-fixed').css('right', '0px')
                                    $('#zbtQuote-modal-backdrop').addClass('in')
                                    $(iframeEle).css('overflow', 'hidden')
                                }, 1000)
                            }
                        }
                    }
                }
            }
            this.onerror = function () {
                setTimeout(function () {
                    zbtweb.onopen = function () {
                        var json = {
                            type: 0,
                            steamId: steamId,
                            accessToken: push_token,
                            platform: 'zbt',
                            messageType: 0,
                            channelType: 99
                        }
                        zbtweb.send(JSON.stringify(json))
                    }
                }, 3000)
            }
        }

    };
    getIframe.prototype.getuserId = function () {
        return this.setting.userId
    };
    getIframe.prototype.geturl = function () {
        return this.setting.url
    };
    getIframe.prototype.getlang = function () {
        return this.setting.lang
    };
    getIframe.prototype.getpushtoken = function () {
        return this.setting.push_token
    };
    getIframe.prototype.getweburl = function () {
        return this.setting.webUrl
    };
    getIframe.prototype.getEle = function () {
        return this.setting.iframeEle
    };
    getIframe.prototype.getSteamId = function () {
        return this.setting.steamId
    };
    getIframe.prototype.getiframeEle = function () {
        return this.setting.iframeEle
    };
    getIframe.prototype.getPlatformId = function () {
        return this.setting.platformId
    }
    getIframe.prototype.getZbtToken = function () {
        return this.setting.zbtToken
    }
    // 删除
    getIframe.prototype.delete = function () {
        $(this.setting.iframeEle).find('.zbtQuote-fixed').remove()
    };
    // 出现
    getIframe.prototype.zbtquote = function (e) {
        var steamId = this.setting.steamId
        var webUrl = this.setting.webUrl;
        var push_token = this.setting.push_token;
        var lang = this.setting.lang;
        var url = this.setting.url;
        var iframeEle = this.setting.iframeEle;
        var userId = this.setting.userId;
        var platformId = this.setting.platformId
        var zbtToken = this.setting.zbtToken
        if (e == 'hide') {
            $(iframeEle).find('.zbtQuote-fixed').css('right', '-436px')
            $(iframeEle).find('#zbtQuote-modal-backdrop').removeClass('in')
            $(iframeEle).css('overflow', 'unset')
            setTimeout(function () {
                $(iframeEle).find('#zbtQuote-modal-backdrop').remove()
                $(iframeEle).find('.zbtQuote-fixed').remove()
            }, 300)
            if (window.localStorage.getItem('zbtQuote') == 'false') {
                return false
            }
            setTimeout(function () {
                zbtweb = new WebSocket(webUrl);
                zbtweb.onopen = function () {
                    var json = {
                        type: 0,
                        steamId: steamId,
                        accessToken: push_token,
                        platform: 'zbt',
                        messageType: 0,
                        channelType: 88,
                        userId: userId,
                        roomNo: userId
                    }
                    zbtweb.send(JSON.stringify(json))
                    zbtwebset = setInterval(function () {
                        zbtweb.send(JSON.stringify(json))
                    }, 50000)
                    zbtweb.onmessage = function (evt) {
                        var res = JSON.parse(evt.data)
                        if (res.result) {
                            var result = JSON.parse(res.result)
                            if (result.type == 'offer' || result.type == 'order') {
                                var zbtwebispass = false
                                if (result.type == 'offer') {
                                    var offers = JSON.parse(result.data.offers)
                                    for (var i = 0; i < offers.length; i++) {
                                        if (offers[i].status != 3 || offers[i].status != 4) {
                                            zbtwebispass = true
                                        }
                                    }
                                }
                                if (result.type == 'order') {
                                    var orders = JSON.parse(result.data.orders)
                                    for (var i = 0; i < orders.length; i++) {
                                        if (orders[i].status != 0) {
                                            zbtwebispass = true
                                        }
                                    }
                                }
                                if (zbtwebispass) {
                                    var iframeUrl = url + '/platform/default/quote.html?' + 'steamId=' + steamId + '&push_token=' + push_token + '&lang=' + lang + '&userId=' + userId + '&platform=' + platformId + '&zbtToken=' + zbtToken;
                                    var iframeId = 'zbtQuote';
                                    var iframeClass = '';
                                    var selecttext = "是否自动弹出"
                                    if (lang != 'zh-CN') {
                                        selecttext = "Auto Pop-up"
                                    }
                                    var selectdiv = '<div class="zbtQuote-icheck"><input type="checkbox" checked="checked" id="zbtQuote-isauto"><label for="zbtQuote-isauto">' + selecttext + '</label> </div>'
                                    var myIframe = '<div class="zbtQuote-fixed">' + selectdiv + '<iframe src="' + iframeUrl + '"id="' + iframeId + '"class="' + iframeClass + '"iframe></div>'
                                    if ($('body').find('#zbtQuote').length >= 1) {
                                        return false
                                    }
                                    if (zbtweb) {
                                        zbtweb.close()
                                    }
                                    clearInterval(zbtwebset)
                                    $(iframeEle).append(myIframe)
                                    var backdrop = '<div id="zbtQuote-modal-backdrop" class="fade"></div>'
                                    $(iframeEle).append(backdrop)
                                    setTimeout(function () {
                                        $(iframeEle).find('.zbtQuote-fixed').css('right', '0px')
                                        $('#zbtQuote-modal-backdrop').addClass('in')
                                        $(iframeEle).css('overflow', 'hidden')
                                    }, 1000)
                                }
                            }
                        }
                    }
                    this.onerror = function () {
                        setTimeout(function () {
                            zbtweb.onopen = function () {
                                var json = {
                                    type: 0,
                                    steamId: steamId,
                                    accessToken: push_token,
                                    platform: 'zbt',
                                    messageType: 0,
                                    channelType: 99
                                }
                                zbtweb.send(JSON.stringify(json))
                            }
                        }, 3000)
                    }
                }
            }, 2000)
        } else if ('show') {
            if ($('body').find('#zbtQuote').length >= 1) {
                return false
            }
            if (zbtweb) {
                zbtweb.close()
            }
            clearInterval(zbtwebset)
            var iframeUrl = url + '/platform/default/quote.html?' + 'steamId=' + steamId + '&push_token=' + push_token + '&lang=' + lang + '&userId=' + userId + '&platform=' + platformId + '&zbtToken=' + zbtToken;
            var iframeId = 'zbtQuote';
            var iframeClass = '';
            var selecttext = "是否自动弹出"
            if (lang != 'zh-CN') {
                selecttext = "Auto Pop-up"
            }
            var selectdiv = '<div class="zbtQuote-icheck"><input type="checkbox" checked="checked" id="zbtQuote-isauto"><label for="zbtQuote-isauto">' + selecttext + '</label> </div>'
            if (window.localStorage.getItem('zbtQuote') == 'false') {
                var selectdiv = '<div class="zbtQuote-icheck"><input type="checkbox" id="zbtQuote-isauto"><label for="zbtQuote-isauto">' + selecttext + '</label> </div>'
            }
            var myIframe = '<div class="zbtQuote-fixed">' + selectdiv + '<iframe src="' + iframeUrl + '"id="' + iframeId + '"class="' + iframeClass + '"iframe></div>'
            $(iframeEle).append(myIframe)
            var backdrop = '<div id="zbtQuote-modal-backdrop" class="fade"></div>'
            $(iframeEle).append(backdrop)
            setTimeout(function () {
                $(iframeEle).find('.zbtQuote-fixed').css('right', '0px')
                $('#zbtQuote-modal-backdrop').addClass('in')
                $(iframeEle).css('overflow', 'hidden')
            }, 1000)
        }
    };
    $(document).on('change', '#zbtQuote-isauto', function () {
        if ($(this).is(':checked')) {
            window.localStorage.setItem('zbtQuote', 'auto')
        } else {
            window.localStorage.setItem('zbtQuote', 'false')
        }
    })
    window.getIframe = getIframe;
})(window, jQuery)


